//
//  PopupView.m
//  Dispatch
//
//  Created by hyeyeon on 14. 1. 13..
//  Copyright (c) 2014년 hyeyeon. All rights reserved.
//

#import "PopupView.h"
#import "AppDelegate.h"

@implementation PopupView
@synthesize delegate;

+(PopupView *)getPopupViewWithDelegate:(id)_delegate{
    return [[PopupView alloc] initWithFrame:CGRectMake(0, 0, mySize.width, mySize.height) andDelegate:_delegate];
}
- (id)initWithFrame:(CGRect)frame andDelegate:(id)_delegate
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        appDelegate = (AppDelegate*)[[UIApplication sharedApplication]delegate];
        self.delegate = _delegate;
        [self makeObject];
    }
    return self;
}

#pragma mark - create
-(void)makeObject{
    
    //배경
    backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, mySize.width, mySize.height)];
    [self addSubview:backgroundView];
    
    UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, backgroundView.frame.size.width, backgroundView.frame.size.height)];
    bgView.backgroundColor = ColorFromRGB(0x3d3d3d);
    bgView.alpha = 0.95f;
    [backgroundView addSubview:bgView];
    
    //팝업
    bodyView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 0)];
    bodyView.backgroundColor = ColorFromRGB(0xf2f2f2);
    bodyView.layer.cornerRadius = 5.0f;
    [backgroundView addSubview:bodyView];
    
    //라벨
    messageLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 40, bodyView.frame.size.width-20, 0)];
    messageLabel.numberOfLines = 0;
    messageLabel.backgroundColor = [UIColor clearColor];
    messageLabel.textColor = ColorFromRGB(0x1c1a1c);
    messageLabel.font = [UIFont systemFontOfSize:15];
    messageLabel.textAlignment = NSTextAlignmentCenter;
    [bodyView addSubview:messageLabel];
    
    //버튼
    firstButton = [[UIButton alloc] initWithFrame:CGRectZero];
    [firstButton setTag:0];
    [firstButton addTarget:self action:@selector(popupButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [firstButton setTitleColor:ColorFromRGB(0xffffff) forState:UIControlStateNormal];
    [firstButton setBackgroundImage:[UIImage imageNamed:@"btn_popup_gray"] forState:UIControlStateNormal];
    [bodyView addSubview:firstButton];
    
    secondButton = [[UIButton alloc] initWithFrame:CGRectZero];
    [secondButton setTag:1];
    [secondButton addTarget:self action:@selector(popupButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [secondButton setTitleColor:ColorFromRGB(0xffffff) forState:UIControlStateNormal];
    [secondButton setBackgroundImage:[UIImage imageNamed:@"btn_popup_blue"] forState:UIControlStateNormal];
    [bodyView addSubview:secondButton];
    
}

#pragma mark - 팝업그리기
-(void)appearMessage:(NSString *)message withFirstButton:(NSString *)first andSecondButton:(NSString *)second andTag:(NSInteger)tag{
    
    self.tag = tag;
    
    messageLabel.text = message;
    [messageLabel sizeToFit];
    
    float height = messageLabel.frame.size.height+40+40+35;
    bodyView.frame = CGRectMake(0, 0, bodyView.frame.size.width, height);
    bodyView.center = CGPointMake(mySize.width/2.0f, mySize.height/2.0f);
    
    messageLabel.center = CGPointMake(bodyView.frame.size.width*0.5f, 40+(messageLabel.frame.size.height*0.5f));
    
    
    firstButton.frame = CGRectMake(0, bodyView.frame.size.height-35, 150, 35);
    [firstButton setTitle:first forState:UIControlStateNormal];
    
    secondButton.frame = CGRectMake(firstButton.frame.origin.x+firstButton.frame.size.width, firstButton.frame.origin.y, 150, 35);
    [secondButton setTitle:second forState:UIControlStateNormal];
    
    
    
    if (![self superview]) {
        [appDelegate.window addSubview:self];
    }
}


#pragma mark - 버튼 클릭
-(void)popupButtonAction:(id)sender{
    
    
    UIButton *button = (UIButton*)sender;
    if ([delegate respondsToSelector:@selector(popupView:clickIndex:)]) {
        [delegate popupView:self clickIndex:button.tag];
    }
    [self removePopup];
}

#pragma mark - 닫기
-(void)removePopup{
    if ([self superview]) {
        [self removeFromSuperview];

    }
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
